
//	ipar = [m, n, outputs on constellation]
//	m is length of x
//	n is length of y
//	rpar = [x1, x2...xm, y1, y2 ... yn,]
//	(x(i), y(j)) is coordinate of a signal on contellation.
//	dstate(1) pending output.
//	dstate(2 ... m+1) bandary of x
//	dstate(m+2 .. m+n+1) bandary of y

function block = qam_demod_com(block, flag)
	select flag 
		case 1 then // output
			block.outptr(1) = block.z(1);
		case 2 then
			//disp("flag = "+string(flag))
			//pause
			m = block.ipar(1);
			n = block.ipar(2);
			for i = 1:m
				if block.inptr(1)(1) <= block.z(i+1)
					im = i;
					break;
				end
			end
			for i = 1:n
				if block.inptr(2)(1) <= block.z(m+i+1)
					in = i;
					break;
				end
			end
			block.z(1) = block.ipar(2+(im-1)*n + in);
		case 4 then
			//disp("flag = "+string(flag))
			//pause
			m = block.ipar(1);
			n = block.ipar(2);
			for i = 1:m-1
				block.z(i+1) = (block.rpar(i)+block.rpar(i+1))/2
			end
			block.z(m+1) = %inf;
			for i = 1:n-1
				block.z(i+m+1) = (block.rpar(i+m) + block.rpar(i+m+1))/2;
			end
			block.z(m+n+1) = %inf;
		end
endfunction

